delphi7点击cxgrid后新增数据时报错?报‘datasetnotineditorinsertmode’,请大家帮个忙,我怎么查也查不出错在哪里,不点击cxgrid,新增数据就
delphi 7 点击cxgrid后 新增数据时报错?报‘dataset not in edit or insert mode’,请大家帮个忙,我怎么查也查不出错在哪里,不点击cxgrid,新增数据就没问题!先谢谢大家!
代码如下:
procedure TForm1.btaddClick(Sender: TObject);
var tmpxmdh,tmpajh,tmpqx,tmpschnd:string;
sy,sm,sd:word;
begin
if btadd.Caption='增加' then
begin
pub_bgqx:=prvbgqx;
pub_class_id:=prvflh;
DecodeDate(now,sy,sm,sd);
with adod_wjj do
begin
close;
commandtext:='select * from wscl_wsda_file order by wjjh desc';
open;
end;
adod_wjj.Append;
cxdbtextedit11.SetFocus;
cxdbtextedit13.DataBinding.DataSource.DataSet.FieldByName('ys').AsInteger:=0;
cxdbtextedit14.DataBinding.DataSource.DataSet.FieldByName('wjfs').AsInteger:=1;
cxdbcombobox6.DataBinding.DataSource.DataSet.FieldByName('bzbm').AsString:=prvbzbm;
cxdbcombobox7.DataBinding.DataSource.DataSet.FieldByName('ztlx').AsString:=prvztlx;
cxdbcombobox9.DataBinding.DataSource.DataSet.FieldByName('mj').AsString:=prvmj;
cxdbtextedit19.DataBinding.DataSource.DataSet.FieldByName('cfwz').AsString:=prvcfwz;
cxdbtextedit18.DataBinding.DataSource.DataSet.FieldByName('wjhh').AsString:=prvhh;
cxdbtextedit28.DataBinding.DataSource.DataSet.FieldByName('flh').AsString:=prvflh;
cxdbtextedit17.DataBinding.DataSource.DataSet.FieldByName('wjnd').AsString:=prvwjnd;
cxdbcombobox8.DataBinding.DataSource.DataSet.FieldByName('bgqx').AsString:=pub_bgqx;
cxdbdateedit6.DataBinding.DataSource.DataSet.FieldByName('gdsj').value:=date;
btadd.Caption:='保存';
btmod.Caption:='还原';
end
else
begin
if ((trim(cxdbtextedit17.Text)='') or (trim(cxdbcombobox8.Text)='') or (trim(cxdbtextedit29.Text)='')) then
begin
MessageBox(0, '文件保管期限、文件年度、文件时间不能为空!', '操作提示:', MB_ICONINFORMATION + MB_OK);
exit;
end;
prvbzbm:=trim(cxdbcombobox6.Text);
prvflh:=trim(cxdbtextedit28.Text);
prvwjnd:=trim(cxdbtextedit17.Text);
prvhh:=trim(cxdbtextedit18.Text);
prvztlx:=trim(cxdbcombobox7.Text);
prvbgqx:=trim(cxdbcombobox8.Text);
prvmj:=trim(cxdbcombobox9.Text);
prvcfwz:=trim(cxdbtextedit19.Text);
with adod_wjj do
begin
fieldbyname('bzbm').Value:=trim(cxdbcombobox6.Text);
fieldbyname('ys').Value:=strtoint(trim(cxdbtextedit13.Text));
fieldbyname('wjfs').Value:=strtoint(trim(cxdbtextedit14.Text));
fieldbyname('flh').Value:=trim(cxdbtextedit28.Text);
fieldbyname('wjnd').Value:=trim(cxdbtextedit17.Text);
fieldbyname('wjhh').Value:=trim(cxdbtextedit18.Text);
fieldbyname('cfwz').Value:=trim(cxdbtextedit19.Text);
tmpschnd:=trim(cxdbtextedit17.Text);
fieldbyname('ztlx').Value:=trim(cxdbcombobox7.Text);
fieldbyname('gdsj').Value:=cxdbdateedit6.date;
fieldbyname('bgqx').Value:=trim(cxdbcombobox8.Text);
fieldbyname('mj').Value:=trim(cxdbcombobox9.Text);
tmpqx:=trim(cxdbcombobox8.Text);
fieldbyname('gdbz').Value:='未归档';
fieldbyname('operdate').Value:=now;
post;
end;
with adod_wjj do
begin
close;
commandtext:='select * from wscl_wsda_file where wjnd=:tnd and bgqx=:tqx order by bgqx,wjjh desc';
parameters[0].DataType:=ftstring; parameters[1].DataType:=ftstring;
parameters[0].Value:=tmpschnd; parameters[1].Value:=tmpqx;
open;
end;
btadd.Caption:='增加';
btmod.Caption:='修改';
end;
end;
procedure TForm1.cxGrid3DBTableView1CellClick(
Sender: TcxCustomGridTableView;
ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
AShift: TShiftState; var AHandled: Boolean);
begin
with adod_wjj do
begin
prvbzbm:=fieldbyname('bzbm').AsString;
prvflh:=fieldbyname('flh').AsString;
prvwjnd:=fieldbyname('wjnd').AsString;
prvhh:=fieldbyname('wjhh').AsString;
prvztlx:=fieldbyname('ztlx').AsString;
prvbgqx:=fieldbyname('bgqx').AsString;
prvmj:=fieldbyname('mj').AsString;
prvcfwz:=fieldbyname('cfwz').AsString;
prvbgqxcode:=fieldbyname('bgqxcode').AsString;
end;
end;
12 个解决方案
提示是你保存的时候ADO没有处于 增加或修改的状态。所以会有异常,
你可以在最前面加一名:
if not (adod_wjj.State in [dsEdit,dsInsert]) then exit;
就不会有这个错误了。
首先确定adod_wjj是不是grid所绑定的数据集
再有,你的赋值过程也太乱了,直接用adod_wjj.FieldByName去赋值,干吗还通过grid一系列属性去赋值
再有,当不满足if btadd.Caption='增加' then 条件进入else后,adod_wjj根本没有Edit,就去为字段赋值了,这样肯定出错
如果上面问题都不是,就逐步跟踪一下,看看在那句出错了
mrwangfei 我是在新增时报的错,不是保存时,只要不点击cxgrid就没问
在赋值时报的错,但这些语句,在没点击cxgrid时,都没问题
cxdbtextedit13.DataBinding.DataSource.DataSet.FieldByName('ys').AsInteger:=0;
cxdbtextedit14.DataBinding.DataSource.DataSet.FieldByName('wjfs').AsInteger:=1;
cxdbcombobox6.DataBinding.DataSource.DataSet.FieldByName('bzbm').AsString:=prvbzbm;
cxdbcombobox7.DataBinding.DataSource.DataSet.FieldByName('ztlx').AsString:=prvztlx;
cxdbtextedit13.DataBinding.DataSource.DataSet.FieldByName('ys').AsInteger:=0;
cxdbtextedit14.DataBinding.DataSource.DataSet.FieldByName('wjfs').AsInteger:=1;
cxdbcombobox6.DataBinding.DataSource.DataSet.FieldByName('bzbm').AsString:=prvbzbm;
cxdbcombobox7.DataBinding.DataSource.DataSet.FieldByName('ztlx').AsString:=prvztlx;
cxdbcombobox9.DataBinding.DataSource.DataSet.FieldByName('mj').AsString:=prvmj;
cxdbtextedit19.DataBinding.DataSource.DataSet.FieldByName('cfwz').AsString:=prvcfwz;
cxdbtextedit18.DataBinding.DataSource.DataSet.FieldByName('wjhh').AsString:=prvhh;
cxdbtextedit28.DataBinding.DataSource.DataSet.FieldByName('flh').AsString:=prvflh;
cxdbtextedit17.DataBinding.DataSource.DataSet.FieldByName('wjnd').AsString:=prvwjnd;
cxdbcombobox8.DataBinding.DataSource.DataSet.FieldByName('bgqx').AsString:=pub_bgqx;
cxdbdateedit6.DataBinding.DataSource.DataSet.FieldByName('gdsj').value:=date;
这些只设一次就行啦
to : dinoalex
cxdbtextedit13.DataBinding.DataSource.DataSet.FieldByName('ys').AsInteger:=0;
cxdbtextedit14.DataBinding.DataSource.DataSet.FieldByName('wjfs').AsInteger:=1;
cxdbcombobox6.DataBinding.DataSource.DataSet.FieldByName('bzbm').AsString:=prvbzbm;
cxdbcombobox7.DataBinding.DataSource.DataSet.FieldByName('ztlx').AsString:=prvztlx;
cxdbcombobox9.DataBinding.DataSource.DataSet.FieldByName('mj').AsString:=prvmj;
cxdbtextedit19.DataBinding.DataSource.DataSet.FieldByName('cfwz').AsString:=prvcfwz;
cxdbtextedit18.DataBinding.DataSource.DataSet.FieldByName('wjhh').AsString:=prvhh;
cxdbtextedit28.DataBinding.DataSource.DataSet.FieldByName('flh').AsString:=prvflh;
cxdbtextedit17.DataBinding.DataSource.DataSet.FieldByName('wjnd').AsString:=prvwjnd;
cxdbcombobox8.DataBinding.DataSource.DataSet.FieldByName('bgqx').AsString:=pub_bgqx;
cxdbdateedit6.DataBinding.DataSource.DataSet.FieldByName('gdsj').value:=date;
这些只设一次就行啦
是什么意思?请指明
现在问题是在新增数据赋值时报错!
那就这样就行了,看来要你改赋值的形式你是不干的。
procedure TForm1.cxGrid3DBTableView1CellClick(
Sender: TcxCustomGridTableView;
ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
AShift: TShiftState; var AHandled: Boolean);
begin
with adod_wjj do
begin
insert;//或 edit;
prvbzbm:=fieldbyname('bzbm').AsString;
prvflh:=fieldbyname('flh').AsString;
prvwjnd:=fieldbyname('wjnd').AsString;
prvhh:=fieldbyname('wjhh').AsString;
prvztlx:=fieldbyname('ztlx').AsString;
prvbgqx:=fieldbyname('bgqx').AsString;
prvmj:=fieldbyname('mj').AsString;
prvcfwz:=fieldbyname('cfwz').AsString;
prvbgqxcode:=fieldbyname('bgqxcode').AsString;
Post;//保存,
end;
end;
to mrwangfei:
以下是取值语句,不是给数据库赋值
procedure TForm1.cxGrid3DBTableView1CellClick(
Sender: TcxCustomGridTableView;
ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
AShift: TShiftState; var AHandled: Boolean);
begin
with adod_wjj do
begin
insert;//或 edit;
prvbzbm:=fieldbyname('bzbm').AsString;
prvflh:=fieldbyname('flh').AsString;
prvwjnd:=fieldbyname('wjnd').AsString;
prvhh:=fieldbyname('wjhh').AsString;
prvztlx:=fieldbyname('ztlx').AsString;
prvbgqx:=fieldbyname('bgqx').AsString;
prvmj:=fieldbyname('mj').AsString;
prvcfwz:=fieldbyname('cfwz').AsString;
prvbgqxcode:=fieldbyname('bgqxcode').AsString;
Post;//保存,
end;
end;
终于找出问题了,是‘cxdbtextedit11.SetFocus;’出问题,把该语句注消掉,就没问题,为什么会这样呢??请大家解释下
终于找出问题了,我用的是 tspeedbutton按钮,换成tbutton就没问题了,这问题折腾了我整整一天。
为什么会这样呢?难道在speedbutton按钮中不能用 setfocus语句???